package cn.wu.reggie.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.wu.reggie.common.Page;
import cn.wu.reggie.common.R;
import cn.wu.reggie.dto.SetmealDto;
import cn.wu.reggie.entity.Category;
import cn.wu.reggie.entity.Setmeal;
import cn.wu.reggie.entity.SetmealDish;
import cn.wu.reggie.exception.BusinessException;
import cn.wu.reggie.mapper.SetmealMapper;
import cn.wu.reggie.query.SetmealQuery;
import cn.wu.reggie.service.ICategoryService;
import cn.wu.reggie.service.ISetmealDishService;
import cn.wu.reggie.service.ISetmealService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ISetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {

    @Autowired
    private ISetmealDishService setmealDishService;

    @Autowired
    private ICategoryService categoryService;

    @Override
    public R saveWithSetmeal(SetmealDto setmealDto) {

        // 1 设置套餐的基本信息 并保存套餐
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(setmealDto.getCreateTime());
        // 保存
        save(setmealDto);
        // 2 获取套餐对应的菜品 批量插入菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        if (CollUtil.isNotEmpty(setmealDishes)) {
            setmealDishes.forEach(sd->{
                //给菜品设置套餐id,排序,俩用户,俩时间
                sd.setSetmealId(setmealDto.getId());
                sd.setSort(0);

                sd.setCreateUser(setmealDto.getCreateUser());
                sd.setUpdateUser(setmealDto.getUpdateUser());

                sd.setCreateTime(setmealDto.getCreateTime());
                sd.setUpdateTime(setmealDto.getUpdateTime());
            });
            // 保存
            setmealDishService.saveBatch(setmealDishes);
        }

        return R.success(null);
    }

    @Override
    public R findByPage(SetmealQuery qo) {
        // 1 设置分页对象
        PageHelper.startPage(qo.getPage(),qo.getPageSize());

        // 2 查询所需的数据
        QueryWrapper<Setmeal> setmealWrapper = new QueryWrapper<>();
        setmealWrapper.eq(qo.getName()!=null,"name",qo.getName());
        List<Setmeal> list = getBaseMapper().selectList(setmealWrapper);
        PageInfo<Setmeal> pageInfo = new PageInfo<>(list);

        // 3 创建dto对象封装返回
        List<SetmealDto> dtoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(list)) {
            list.forEach(s->{
                SetmealDto dto = new SetmealDto();
                BeanUtils.copyProperties(s,dto);
                // 查询分类名称
                Category category = categoryService.getOne(
                        new QueryWrapper<Category>().eq("id", s.getCategoryId()));
                dto.setCategoryName(category.getName());
                dtoList.add(dto);

            });
        }

        return R.success(new Page<>(dtoList,pageInfo.getTotal()));
    }

    @Override
    public R deleteByIds(List<Long> ids) {

        if (CollUtil.isEmpty(ids)) {
            throw new BusinessException("请选择删除的套餐");
        }

        // 查看状态是否为禁用如果不是不能删除
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1).in("id",ids);
        List<Setmeal> setmealList = list(wrapper);
        if (CollUtil.isNotEmpty(setmealList)) {
            throw new BusinessException("有正在售卖的套餐，请停售后重试");
        }

        // 批量删除套餐和套餐菜品关系表
        removeByIds(ids);
        setmealDishService.remove(new QueryWrapper<SetmealDish>()
                .in("setmeal_id",ids));

        return R.success(null);
    }
}
